import { spriteAssetsSprite } from '@renderer/utils/bundle'
import useMainStore from '@renderer/store/modules/useMainStore'
import * as PIXI from 'pixi.js'
import { closeLantern } from '@renderer/views/Accessories'

export default async (title: string): Promise<Record<string, PIXI.Container<PIXI.ContainerChild>>> => {
  const mainStore = useMainStore()
  const { cbWidth, cbHeight } = mainStore.getAppAttr
  const container = new PIXI.Container() // 创建容器
  // 蒙版
  const modalRect = new PIXI.Graphics()
  modalRect.rect(0, 0, cbWidth, cbHeight).fill({
    color: 0x000000,
    alpha: 0.7,
  })
  modalRect.position.set(-cbWidth / 2, 0)
  // 上框
  const upSprite = await spriteAssetsSprite('set_medium_up')
  upSprite.anchor.set(0.5)
  upSprite.position.set(0, upSprite.height / 2 + cbHeight * 0.02)
  // 标题
  const titleText = new PIXI.Text({
    text: title,
    alpha: 0.75,
    style: {
      fontFamily: 'RZFont',
      fontSize: 48,
      fill: '#FFFFFF',
      align: 'center',
      dropShadow: {
        alpha: 0.55,
        distance: 3,
      },
    },
  })
  titleText.anchor.set(0.5)
  titleText.position.set(0, upSprite.position.y + 6)
  // 下框
  const bottomSprite = await spriteAssetsSprite('set_medium_bottom')
  bottomSprite.anchor.set(0.5)
  bottomSprite.position.set(0, cbHeight * 0.98 - bottomSprite.height / 2)
  // 按钮容器
  const buttonsContainer = new PIXI.Container()
  const buttonsContainerSpritee = await spriteAssetsSprite('set_buttons_container')
  buttonsContainerSpritee.anchor.set(0.5)
  buttonsContainer.addChild(buttonsContainerSpritee)
  buttonsContainer.position.set(buttonsContainer.width - upSprite.width / 2, buttonsContainer.height / 2 + upSprite.position.y)
  // 内容区域
  const contentAreaContainer = new PIXI.Container()
  const contentAreaSprite = await spriteAssetsSprite('set_content_area', {
    width: upSprite.width - buttonsContainer.width * 2,
    height: buttonsContainer.height,
  })
  contentAreaSprite.anchor.set(0.5)
  contentAreaContainer.addChild(contentAreaSprite)
  contentAreaContainer.position.set(buttonsContainer.width / 2 - 10, buttonsContainer.height / 2 + upSprite.position.y + 10)
  // 关闭按钮
  const { container: closeLanternSprite, closeContainer } = await closeLantern()
  closeLanternSprite.position.set((upSprite.width - buttonsContainer.width + closeLanternSprite.width) / 2, upSprite.position.y + upSprite.height)
  // 添加到容器
  container.addChild(modalRect)
  container.addChild(buttonsContainer)
  container.addChild(contentAreaContainer)
  container.addChild(upSprite)
  container.addChild(bottomSprite)
  container.addChild(titleText)
  container.addChild(closeLanternSprite)
  container.position.set(cbWidth / 2, 0) // 定位
  container.visible = false // 默认不显示
  return {
    container,
    buttonsContainer,
    closeContainer,
    contentAreaContainer,
  }
}
